package cmd

import (
	"time"

	"gitee.com/zfd81/dbrs/config"

	"github.com/spf13/cobra"
)

const (
	Version     = "1.0"
	Name        = "dbrs"
	Description = "Databroker server"

	defaultDialTimeout      = 2 * time.Second
	defaultCommandTimeOut   = 5 * time.Second
	defaultKeepAliveTime    = 2 * time.Second
	defaultKeepAliveTimeOut = 6 * time.Second
)

var (
	conf        = config.GetConfig()
	globalFlags = GlobalFlags{}
	rootCmd     = &cobra.Command{
		Use:        Name,
		Short:      Description,
		SuggestFor: []string{"dbrs"},
	}
)

func usageFunc(c *cobra.Command) error {
	return UsageFunc(c, Version)
}

func InitCommand() {
	rootCmd.SetUsageFunc(usageFunc)
	// Make help just show the usage
	rootCmd.SetHelpTemplate(`{{.UsageString}}`)

	rootCmd.CompletionOptions.DisableDefaultCmd = true

	rootCmd.AddCommand(
		NewStartCommand(),
		NewTestCommand(),
		NewVersionCommand(),
	)
}

func Execute() {
	InitCommand()
	if err := rootCmd.Execute(); err != nil {
		ExitWithError(ExitError, err)
	}
}
